package admission.ecnu.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import admission.ecnu.db.DBConnection;
import admission.ecnu.object.OTeacherInfo;
import admission.ecnu.po.User;

public class teacherInfoDao {
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	
	public teacherInfoDao() {
		// TODO Auto-generated constructor stub
		this.conn = DBConnection.getConnection();
	}
	public OTeacherInfo teacherSelectById(String teacher_id){
		OTeacherInfo objTeacher = new OTeacherInfo();
		try {
			this.pstmt = this.conn.prepareStatement("select id,name,rank,others from teacher_info where id=?");
			this.pstmt.setString(1,teacher_id);
			this.rs = this.pstmt.executeQuery();

			objTeacher.setTeacherId(rs.getString("id"));
			objTeacher.setName(rs.getString("name"));
			objTeacher.setSpecial(rs.getString("others"));
			objTeacher.setTitle(rs.getString("rank"));

		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				this.rs.close();
				this.pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConnection.closeConnection(this.conn);
		}
		return objTeacher;
	}
	public List<OTeacherInfo> teacherSelect(){
		List<OTeacherInfo> list = new ArrayList<OTeacherInfo>();
		try {
			this.pstmt = this.conn.prepareStatement("select id,name,rank,others from teacher_info");
			this.rs = this.pstmt.executeQuery();
			while(this.rs.next()){
				OTeacherInfo objTeacher = new OTeacherInfo();
				objTeacher.setTeacherId(rs.getString("id"));
				objTeacher.setName(rs.getString("name"));
				objTeacher.setSpecial(rs.getString("others"));
				objTeacher.setTitle(rs.getString("rank"));
				list.add(objTeacher);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				this.rs.close();
				this.pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConnection.closeConnection(this.conn);
		}
		return list;
	}
	
	
	
	public int teacherInsert(String username,String userpass){
		int i = 0;
		try {
			this.pstmt = this.conn.prepareStatement("insert into t_user(username,userpass) value(?,?)");
			this.pstmt.setString(1,username);
			this.pstmt.setString(2,userpass);
			i = this.pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				this.pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConnection.closeConnection(this.conn);
		}
		return i;
	}
	
	public int teacherInsert(OTeacherInfo newTeacher){
		int i = 0;
		try {
			this.pstmt = this.conn.prepareStatement("insert into teacher_info(id,name,rank,others) values (?,?,?,?)");
			this.pstmt.setString(1, newTeacher.getTeacherId());
			this.pstmt.setString(2, newTeacher.getName());
			this.pstmt.setString(3, newTeacher.getTitle());
			this.pstmt.setString(4, newTeacher.getSpecial());
			
			i = this.pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				this.pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConnection.closeConnection(this.conn);
		}
		return i;
	}
	
	public void teacherInsert(List teacherList){
		for(int i = 0; i<teacherList.size(); i++){
			teacherInsert((OTeacherInfo)teacherList.get(i));
		}
	}
	
	public int teacherDelete(String id){
		int i = 0;
		try {
			this.pstmt = this.conn.prepareStatement("delete from teacher_info where id in(?)");
			this.pstmt.setString(1, id);
			i = this.pstmt.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				this.pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			DBConnection.closeConnection(this.conn);
		}
		return i;
	}
	
	public int teacherUpdate(String id,String username,String userpass){
		int i = 0;
		try {
			this.pstmt = this.conn.prepareStatement("update t_user set username=?,userpass=? where id=?");
			this.pstmt.setString(1,	username);
			this.pstmt.setString(2, userpass);
			this.pstmt.setString(3, id);
			i = this.pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				this.pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConnection.closeConnection(this.conn);
		}
		return i;
	}
	
	public void teacherUpdate(List<OTeacherInfo>teacherList){
		
	}
	
	public List<User> teacherUpdateLoad(String id){
		List<User> list = new ArrayList<User>();
		try {
			this.pstmt = this.conn.prepareStatement("select username,userpass from t_user where id = ?");
			this.pstmt.setString(1, id);
			this.rs = this.pstmt.executeQuery();
			while(this.rs.next()){
				User user = new User();
				user.setUsername(rs.getString("username"));
				user.setUserpass(rs.getString("userpass"));
				list.add(user);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				this.pstmt.close();
				this.rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			DBConnection.closeConnection(this.conn);
		}
		return list;
	}
}